home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 4 / The Pier Shareware #4 (The Pier Exchange) (1994).ISO / 038 / prochook.exe / GPA.C < prev    next >
C/C++ Source or Header  |  1994-01-01  |  6KB  |  186 lines

  1. /*
  2.   GPA.C - Copyright (c) 1993 James M. Finnegan, All Rights Reserved
  3. */
  4. #include <windows.h>
  5. #include "gpa.h"
  6. #include "prochook.h"
  7. #include "goodies.h"
  8.  
  9. // Global stuff
  10. HANDLE hInst;
  11. HWND   ghWnd;
  12. // Magic cookie for GetProcAddress hook
  13. NPHOOKCHILD npHookChild;
  14.  
  15.  
  16. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine,
  17.            int nCmdShow)
  18. {
  19.     static char szAppName[]="gpa";
  20.     HWND        hWnd;
  21.     MSG         msg;
  22.     WNDCLASS    wndclass;
  23.  
  24.     
  25.     hInst=hInstance;
  26.     
  27.     if(!hPrevInstance)
  28.     {
  29.         wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  30.         wndclass.lpfnWndProc   = WndProc;
  31.         wndclass.cbClsExtra    = 0;
  32.         wndclass.cbWndExtra    = DLGWINDOWEXTRA;
  33.         wndclass.hInstance     = hInstance;
  34.         wndclass.hIcon         = LoadIcon(hInstance,szAppName);
  35.         wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  36.         wndclass.hbrBackground = COLOR_WINDOW + 1;
  37.         wndclass.lpszMenuName  = NULL;
  38.         wndclass.lpszClassName = szAppName;
  39.  
  40.         if(!RegisterClass(&wndclass))
  41.             return -1;
  42.     }
  43.  
  44.     if((hWnd=CreateDialog(hInstance,szAppName,0,NULL)) == NULL)
  45.         return -1;
  46.  
  47.     // Make HWND global
  48.     ghWnd=hWnd;
  49.     
  50.     ShowWindow(hWnd,nCmdShow);
  51.  
  52.     while(GetMessage(&msg,NULL,0,0))
  53.     {
  54.         if((!IsWindow(hWnd)) ||
  55.            (!IsDialogMessage(hWnd,&msg)))
  56.         {
  57.             TranslateMessage(&msg);
  58.             DispatchMessage(&msg);
  59.         }
  60.     }
  61.  
  62.     return msg.wParam;
  63. }
  64.  
  65.  
  66. long WINAPI WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
  67. {
  68.     FARPROC lpfnNewGetPA;  // Pointer for MakeProcInstance()
  69.     int iTabStop[4];       // Tabs for the list box
  70.                             
  71.                             
  72.     switch(message)
  73.     {
  74.         case WM_CREATE:
  75.             //Center the window on the screen
  76.             CenterWindow(hWnd);
  77.             
  78.             // Set up the hook to our new function
  79.             lpfnNewGetPA=MakeProcInstance((FARPROC)NewGetProcAddress,hInst);
  80.             npHookChild=SetProcAddress((FARPROC)GetProcAddress,lpfnNewGetPA,FALSE);
  81.             
  82.             // Post a message to set the tabs in the list box
  83.             PostMessage(hWnd,WM_SETLBTABS,0,0L);
  84.             break;
  85.         
  86.         case WM_SETLBTABS:
  87.             iTabStop[0]=28;
  88.             iTabStop[1]=83;
  89.             iTabStop[2]=127;
  90.             iTabStop[3]=146;
  91.             SendDlgItemMessage(hWnd,IDL_MAINBOX,LB_SETTABSTOPS,4,(LONG)(LPINT)iTabStop);
  92.  
  93.             SetFocus(GetDlgItem(hWnd,IDB_CLEAR));
  94.             break;
  95.             
  96.         // Processing for the listbox buttons...
  97.         case WM_COMMAND:
  98.             switch(wParam)
  99.             {
  100.                 case IDB_CLEAR:
  101.                     SendDlgItemMessage(hWnd,IDL_MAINBOX,LB_RESETCONTENT,0,0L);
  102.                     break;
  103.  
  104.                 case IDB_EXIT:
  105.                     PostMessage(hWnd,WM_CLOSE,0,0L);
  106.                     break;
  107.             }
  108.             break;
  109.             
  110.         case WM_DESTROY:
  111.             // Delete the reference to the hook
  112.             SetProcRelease(npHookChild);
  113.             PostQuitMessage(0);
  114.             break;
  115.  
  116.         default:
  117.             return DefWindowProc(hWnd, message, wParam, lParam);
  118.             break;
  119.     }
  120.     return 0L;
  121. }
  122.             
  123. /* 
  124.  All the stuff below is called only when GetProcAddress() is called from
  125.  another application! 
  126. */
  127.  
  128. FARPROC __export WINAPI NewGetProcAddress(HINSTANCE hInst,LPCSTR lpszExp)
  129. {
  130.     FARPROC     i;
  131.     static char szItem[100];
  132.     static char szResult[5];
  133.     HTASK       wCurTask;
  134.     HMODULE     wModHandle=NULL;
  135.     
  136.     
  137.     // Unhook our reference
  138.     ProcUnhook(npHookChild);
  139.     // Reissue the call to GetProcAddress 
  140.     i=GetProcAddress(hInst,lpszExp);
  141.     // Rehook our reference
  142.     ProcHook(npHookChild);
  143.  
  144.     // Get the caller's task handle
  145.     wCurTask=GetCurrentTask();
  146.     
  147.     // Get the module handle of the referenced library.
  148.     // Quasi-documented... pass hInst instead of a string as a parameter!
  149.     if(hInst != NULL)
  150.         wModHandle=GetModuleHandle((LPCSTR)hInst);
  151.  
  152.     // Assign some readable text to the result of the call to GetProcAddress
  153.     if(i == NULL)
  154.         lstrcpy(szResult,"Fail");
  155.     else
  156.         lstrcpy(szResult,"OK");
  157.    
  158.     // If the caller passed in an ordinal instead of a string, print the
  159.     // ordinal and search for the string in the EXE header 
  160.     if(HIWORD(lpszExp) == 0)
  161.     {    
  162.         wsprintf(szItem,"%s\t%-8.8s\t%-8.8s\t(%03d)\t%s",(LPSTR)szResult,
  163.                                                          GetTaskName(wCurTask),
  164.                                                          GetModuleName(wModHandle),
  165.                                                          LOWORD(lpszExp),
  166.                                                          GetNameFromOrd(wModHandle,(WORD)LOWORD(lpszExp)));
  167.     }
  168.     // The caller used the "straight and narrow".  Just print the 
  169.     // parameters and the result.
  170.     else
  171.     {    
  172.         wsprintf(szItem,"%s\t%-8.8s\t%-8.8s\t\t%s",(LPSTR)szResult,
  173.                                                    GetTaskName(wCurTask),
  174.                                                    GetModuleName(wModHandle),
  175.                                                    lpszExp);
  176.     }
  177.    
  178.     // Put the string in the listbox.
  179.     SendDlgItemMessage(ghWnd,IDL_MAINBOX,LB_ADDSTRING,0,(LONG)(LPSTR)szItem);
  180.       
  181.     // Return the value returned from the call to GetProcAddress to
  182.     // make us look transparent!
  183.     return i;
  184. }     
  185.  
  186.